<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.clientName')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="clientId">
              <a-select  name="clientId" v-model="model.clientId" :placeholder="$t('common.form.rule.select', { label: $t('columns.clientName') })">
                <a-select-option v-for="(item, index)  in clientIds" :key="item.key" :value="item.value"> {{item.label}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.title')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <a-input v-model="model.title" :placeholder="$t('common.form.rule.input', { label: $t('columns.title') })"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.content')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
              <a-textarea v-model="model.content" rows="4" :placeholder="$t('common.form.rule.input', { label: $t('columns.content') })" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.payload')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="payload">
              <a-textarea v-model="model.payload" rows="4" :placeholder="$t('common.form.rule.input', { label: $t('columns.payload') })" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<i18n>
  {
    "zh-CN": {
      "columns": {
        "clientName": "客户端名称",
        "title": "消息标题",
        "content": "消息内容",
        "payload": "消息负载"
      }
    },
    "en-US": {
      "columns": {
        "clientName": "Client name",
        "title": "Message Title",
        "content": "Message content",
        "payload": "Message Payload"
      }
    },
    "fr-FR": {
      "columns": {
        "clientName": "Nom du client",
        "title": "Titre du message",
        "content": "Contenu du message",
        "payload": "Charge de message"
      }
    }
  }
</i18n>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'AppPushRecordForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/app/push-record/add",
          edit: "/app/push-record/edit",
          queryById: "/app/push-record/queryById",
          clientOptionUrl:"/app/client/options",
        },
        clientIds:[],
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      //备份model原始值
      this.getClientOption();
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      getClientOption(){
        httpAction(this.url.clientOptionUrl,{},'get').then((res)=>{
          this.clientIds=res.result;
        }).finally(() => {
        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>